import axios, { AxiosInstance } from 'axios'

function initInstance(url: string) {

    const axiosInstance: AxiosInstance = axios.create({
        // baseURL 需要设置为反向代理前缀，不能设置绝对路径URL
        baseURL: url,
        timeout: 60000,
        withCredentials: false
    })
    // 实例添加请求拦截器
    axiosInstance.interceptors.request.use((config: any) => {
        // config.headers.storeType = selectTypeStore.type
        // const w: any = window
        // const token = w.$wujie?.props.token
        // config.headers.token = token
        return config
    }, error => {
        return error
    })

    // 响应拦截器
    axiosInstance.interceptors.response.use(response => {
        if (response.status === 200) {
            if (response.headers['content-type']?.indexOf('application/json') == -1) {
                return Promise.resolve(response)
            }
            return Promise.resolve(response.data)
        } else {
            return Promise.reject(response)
        }
    }, error => {
        if (error.response.status) {
            return Promise.reject(error.response)
        }
    })
    return axiosInstance
}

export const baseURL = '/api'
export interface HttpResponse<T = any> {
    ok: boolean;
    code?: string;
    message?: string;
    data?: T;
}

export interface QueryParams<P = any> {
    pageIndex: number;
    pageSize: number;
    filter: P;
}

const instance = initInstance('http://169.254.99.183:28080/tr-cloud-core-gen')

export default instance




